<template>
  <el-container class="home-container">
   <!-- 头部-->
    <el-header>
      <div>
        <img src="../assets/img/heima.png">
        <span>电商管理后台</span>
      </div>
        <el-button plain @click="loginOut">退出</el-button>
    </el-header>
    <!--页面主体-->
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isToggle?'64px':'200px'">
        <div class="toggle-button" @click="toggleMenu">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isToggle"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!--一级菜单-->
          <el-submenu :index="menu.id+''" v-for="menu in menuList" :key="menu.id">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconObj[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>
           <!--二级菜单-->
            <el-menu-item
              :index="'/'+item.path"
              v-for="item in menu.children"
              :key="item.id"
              @click="saveNavState('/'+item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主体-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
    export default {
        name: "Home",
        data(){
            return {
               menuList:[],
               iconObj:{
                  '125':'iconfont icon-users',
                  '103':'iconfont icon-tijikongjian',
                  '101':'iconfont icon-shangpin',
                  '102':'iconfont icon-danju',
                  '145':'iconfont icon-baobiao',
               },
               isToggle:false,
               activePath:''
            }
        },
        created(){
            this.getMenuList();
            this.activePath=window.sessionStorage.getItem('activePath');
        },
        methods:{
            //退出登录
            loginOut(){
              window.sessionStorage.clear();
              this.$router.push('/login');
            },
            //获取所有菜单
            getMenuList(){
                this.$http.get('menus').then(res=>{
                    if(res.data.meta.status==200){
                      this.menuList=res.data.data;
                    }
                })
            },
            //切换菜单折叠与展开
            toggleMenu(){
                this.isToggle=!this.isToggle;
            },
            //session存储当前地址，保存连接激活状态
            saveNavState(path){
                window.sessionStorage.setItem('activePath',path);
               this.activePath=window.sessionStorage.getItem('activePath');
            }
        }
    }
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
  .el-header{
    background: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: white;
    font-size: 20px;
    >div{
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }
  .el-aside{
    background-color: #333744;
    .el-menu{
      border-right: none;
    }
  }
  .el-main{
    background-color: #eaedfe;
  }
  .iconfont{
    margin-right: 8px;
  }
  .toggle-button{
    background-color: #4A5064;
    color: white;
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    letter-spacing: 0.2rem;
  }
  .toggle-button:hover{
    cursor: pointer;
  }
</style>
